package com.dlyk.web;

import com.dlyk.model.DicType;
import com.dlyk.result.R;
import com.dlyk.service.DicTypeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典类型
 */
@RestController
public class DicTypeController {

    @Resource
    private DicTypeService dicTypeService;

    /**
     * 查询所有的字典类型
     */
    @PreAuthorize(value = "hasAuthority('dicType:list')")
    @GetMapping("/api/dicType/all")
    public R dicTypeAll(){

        List<DicType> dicTypeList = dicTypeService.getAllDicType();

        return R.OK(dicTypeList);
    }

    /**
     * 查询字典类型列表（分页查询）
     */
    @PreAuthorize(value = "hasAuthority('dicType:list')")
    @GetMapping("/api/dicType/list")
    public R dicTypeListByPage(@RequestParam(required = false) Integer current) {

        // required = false  表示非必须，这个参数也可以不传，不传参数，默认查询第一页
        if (current == null) {
            current = 1;
        }

        PageInfo<DicType> dicTypePageInfo = dicTypeService.getDicTypeListByPage(current);

        return R.OK(dicTypePageInfo);
    }

    /**
     * 录入字典类型
     */
    @PreAuthorize(value = "hasAuthority('dicType:add')")
    @PostMapping("/api/dicType/add")
    public R addDicType(DicType dicType) {

        // 这里就不使用Query对象接收了，直接使用DicType对象接收

        int add = dicTypeService.addDicType(dicType);

        return add >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑字典类型
     */
    @PreAuthorize(value = "hasAuthority('dicType:edit')")
    @PutMapping("/api/dicType/edit")
    public R editDicType(DicType dicType) {

        // 这里就不使用Query对象接收了，直接使用DicType对象接收

        int edit = dicTypeService.editDicType(dicType);

        return edit >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize(value = "hasAuthority('dicType:remove')")
    @DeleteMapping("/api/dicType/remove/{id}")
    public R removeDicType(@PathVariable Integer id) {

        // 这里就不使用Query对象接收了，直接使用DicType对象接收

        int remove = dicTypeService.removeDicTypeById(id);

        return remove >= 1 ? R.OK() : R.FAIL();
    }

}

